var app = getApp()
import * as Api from '../../utils/api.js'
Page({
  data:{
    set:'off',
    canX:'',
    canY:'',
    canvasW:'',
    canvasH: '',
    windowWidth:'',
    windowHeight:'',
    mysrc:''


  },
  onShow:function(){
    const that = this; 
    // wx.downloadFile({
    //   url: that.data.mysrc,
    //   success: function (sres) {
    //     console.log(sres);
    //     that.data.mysrc = sres.tempFilePath
    //   }, fail: function (fres) {

    //   }
    // })  
  },
  onLoad() {
    let _this = this;
    let windowStroInfo = wx.getStorageSync('windowStroInfo') ? JSON.parse(wx.getStorageSync('windowStroInfo')) : ''
    if (!windowStroInfo) {
      wx.getSystemInfo({
        success: function (res) {

          _this.setData({
            windowWidth: res.windowWidth,
            windowHeight: res.windowHeight
          })
          wx.setStorageSync('windowStroInfo', JSON.stringify({ width: res.windowWidth, height: res.windowHeight }))

        }
      })
    } else {
      _this.setData({
        windowWidth: windowStroInfo.width,
        windowHeight: windowStroInfo.height
      })
    }

    this.queryMultipleNodes()
  },
  // 获取框的节点信息
  queryMultipleNodes: function () {
    let _this = this;
    var query = wx.createSelectorQuery()
    query.select('#the-id').boundingClientRect()
    query.selectViewport().scrollOffset()
    query.exec(function (res) {
      console.log(res)
      _this.setData({
        canX: res[0].left,
        canY: parseInt(res[0].top),
        canvasW: res[0].width,
        canvasH: res[0].height,
      })
    })
  },
  // 截取当前框的图片 调用画布
  drawImg(){
    let _this = this
    const ctx = wx.createCanvasContext('myCanvas')
    ctx.drawImage(this.data.mysrc, 0, 0, this.data.windowWidth, this.data.windowHeight);
    ctx.draw(true, () => {
      var canvasW = _this.data.canvasW
      var canvasH = _this.data.canvasH
      var canvasL = _this.data.canX
      var canvasT = _this.data.canY
      wx.canvasToTempFilePath({
        x: canvasL,
        y: canvasT,
        width: canvasW,
        height: canvasH,
        destWidth: canvasW,
        destHeight: canvasH,
        quality: 0.5,
        canvasId: 'myCanvas',
        success: function (res) {
          // 成功获得裁剪后图地址
          console.log(res.tempFilePath)
          console.log('裁剪555')
          wx.hideLoading()
          wx.navigateBack({
            delta:1 
          })
          wx.setStorageSync('tempFilePath', res.tempFilePath)
          //_this.setImg(res.tempFilePath)
          
        }
      })
    })
  },
  setImg(img){
    let url = app.globalData.baseUrl1 + '/fileupload'
    let uploadTask = wx.uploadFile({
      url: url,
      filePath: img,
      name: 'file',
      success: function (res1) {
        console.log(res1)
        console.log(111)
        let opt = JSON.parse(res1.data)
        console.log(opt.data.filename)
        console.log(9999)
        // wx.redirectTo({
        //   url: '/pages/example/querycar/querycar?imgIcon=' + opt.data.filename,
        // })
        wx.setStorageSync('tempFilePath', opt.data.filename)
        wx.navigateBack({
          delta:1
        })
        app.globalData.textImg = opt.data.filename
      },
    })
  },
  // canvas 画布截取
  takePhoto() {
    let _this = this
    const ctx = wx.createCameraContext()
    console.log(ctx)
    if (wx.getStorageSync('cameraImg') ){
      wx.removeStorageSync('cameraImg')
    }
    wx.showLoading({
      title: '截取中...',
    })
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        console.log(res)
        _this.data.mysrc = res.tempImagePath
        _this.drawImg()
      },
      fail:(err) => {
        console.log(err)
        wx.hideLoading()
      }
    })
  },
  getCamera(){
    wx.chooseImage({
      count: 1, // 默认9
      sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log(res)
        // wx.redirectTo({
        //   url: '/pages/example/querycar/querycar?imgIcon='+res.tempFilePaths[0],
        // })
        console.log(res.tempFilePaths[0])
        console.log('传的图片')
        wx.setStorageSync('cameraImg',true)
        wx.setStorageSync('tempFilePath', res.tempFilePaths[0])
        wx.navigateBack({
          delta: 1
        })
      }
    })
  }
  
})